package view;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import model.ContentBlock;
import util.MemoryManage;
import model.PageTable;
import model.DiskBlock;
import model.Index;

import java.util.Map;
import java.util.Optional;

/**
 * @Create Harlantown
 * @Description
 * @create 2022-12-30 下午 10:51
 */
public class pageReaderView {
    // 磁盘块（指针）
    private DiskBlock[] diskBlocks;
    // 当前进程打开的文件名
    private String fileName;
    // 当前进程的页表
    private PageTable pageTable;
    // 当前进程打开文件的总长度
    private int totalLength;
    // 当前进程的磁盘索引表
    Map<Integer, Integer> indexMap;
    // 关联javafx的具体组件
    @FXML
    private Button readBtn;
    @FXML
    private TextField targetField;
    @FXML
    private TextField name;
    @FXML
    private TextField totalNum;
    @FXML
    private TextArea showArea;

    public void start() {

        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ui/readpage.fxml"));
            Pane root = fxmlLoader.load();
            Scene scene = new Scene(root);
            Stage primaryStage = new Stage();

            name = (TextField) root.lookup("#name");
            name.setText(fileName);

            totalNum = (TextField) root.lookup("#totalNum");
            totalNum.setText(String.valueOf(totalLength));

            targetField = (TextField) root.lookup("#targetField");

            showArea = (TextArea) root.lookup("#showArea");

            readBtn = (Button) root.lookup("#readBtn");
            readBtn.setOnAction(ActionEvent -> {
                String text = targetField.getText();
                int page = Integer.parseInt(text);

                // 具体逻辑交给下级
                ContentBlock contentBlock = MemoryManage.readFromMemory(pageTable, page, diskBlocks, indexMap);
                if (contentBlock != null) {
                    if (contentBlock.getContent() != null) {
                        showArea.appendText("block" + page + ":" + contentBlock.getContent() + "\n");
                    }
                }


            });

            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                    //对话框 Alert Alert.AlertType.CONFIRMATION：反问对话框
                    Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
                    //设置对话框标题
                    alert2.setTitle("Exit");
                    //设置内容
                    alert2.setHeaderText("关闭文件并清理内存？");
                    //显示对话框
                    Optional<ButtonType> result = alert2.showAndWait();
                    //如果点击OK
                    if (result.get() == ButtonType.OK) {
                        // ... user chose OK
                        primaryStage.close();
                        MemoryManage.removeFile(pageTable);
                    } else {
                        event.consume();
                    }
                }
            });

            primaryStage.setScene(scene);
            primaryStage.setTitle(fileName);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public pageReaderView(DiskBlock[] diskBlocks, String fileName, Index index, int totalLength, FileView fileView) {
        this.fileName = fileName;
        this.totalLength = totalLength;
        this.diskBlocks = diskBlocks;
        this.pageTable = new PageTable();
        //
        int startBlock = MemoryManage.allocateMemory(pageTable);
        this.indexMap = index.getIndexMap();
        if (startBlock == -1) {
            Alert erralert = new Alert(Alert.AlertType.ERROR);
            erralert.setHeaderText(null);
            erralert.setContentText("内存不足！");
            erralert.showAndWait();
        }

    }

    public pageReaderView() {

    }

    public void visitPage() {

    }
}
